package com.mazaiting.classpath;

import cn.hutool.core.util.ObjectUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.LinkedList;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * 分割符路径类读取
 *
 * @author mazaiting
 * @since 2021/8/10
 */
@Slf4j
@Data
public class CompositeEntry implements Entry {
    /**
     * 组合读取类列表
     */
    private LinkedList<Entry> compositeEntryList;

    public CompositeEntry(String pathList) {
        for (String path : pathList.split(File.pathSeparator)) {
            compositeEntryList.add(EntryFactory.newEntry(path));
        }
    }

    @Override
    public ClassResponse readClass(String className) {
        for (Entry entry : this.compositeEntryList) {
            ClassResponse classResponse = entry.readClass(className);
            if (ObjectUtil.isEmpty(classResponse.getError())) {
                return classResponse;
            }
        }
        ClassResponse classResponse = new ClassResponse();
        classResponse.setError("class not found: " + className);
        return classResponse;
    }

    @Override
    public String toString() {
        return StringUtils.join(
                this.compositeEntryList
                        .stream()
                        .map(Entry::toString)
                        .toArray(), File.pathSeparator);
    }
}


















